package com.zh.work.controller.platform.branch;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.work.entity.branch.BranchSpreadEntity;
import com.zh.work.service.platform.branch.BranchHelpService;
import com.zh.common.entity.JSONResult;
import com.zh.common.annotaion.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 机构文案营销管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/11/02
 * @Version: 1.0.1
 */
@Api(value = "机构文案营销管理控制器", produces = "application/json", tags = {"机构文案营销管理控制器"})
@RestController
@RequestMapping("/spread")
public class BranchSpreadController {


    @Autowired
    private BranchHelpService service;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/11/02
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "新增一条记录", tags = {"putAddBranchSpread"}, notes = "新增一条记录")
    @PutMapping("/putAddBranchSpread")
    @ResponseBody
    public JSONResult<Object> putAddBranchSpread(@ApiParam(value = "机构文案营销实体类", name = "branchSpread")@RequestBody BranchSpreadEntity branchSpreadEntity,
                                              @CurrentUser Principal principal) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        branchSpreadEntity.setLoginName(principal.getName());
        try {
            if (this.service.addSpread(branchSpreadEntity) > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModBranchSpreadById"}, notes = "通过id修改信息")
    @PutMapping("/putModBranchSpreadById")
    @ResponseBody
    public JSONResult<Object> putModBranchSpreadById(@ApiParam(value = "机构文案营销实体类", name = "branchSpread") @RequestBody BranchSpreadEntity branchSpreadEntity,
                                                     @CurrentUser Principal principal) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        branchSpreadEntity.setLoginName(principal.getName());
        try {

            if (service.updateBranchSpreadById(branchSpreadEntity) > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "根据主键删除记录", tags = {"deleteBranchSpreadById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteBranchSpreadById")
    public JSONResult<Object> deleteBranchSpreadById(@ApiParam(value = "机构文案营销实体类", name = "branchSpread") BranchSpreadEntity branchSpreadEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (service.deleteBranchSpreadById(branchSpreadEntity.getId()) > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteBranchSpreadBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteBranchSpreadBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteBranchSpreadBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody @RequestParam("ids") List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.service.deleteBranchSpreadBatchByIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getBranchSpreadById"}, notes = "根据id查询详细信息")
    @GetMapping("/getBranchSpreadById")
    public JSONResult<BranchSpreadEntity> getBranchSpreadById(@ApiParam(value = "机构文案营销实体类", name = "branchSpread") BranchSpreadEntity branchSpreadEntity) {
        JSONResult<BranchSpreadEntity> jsonResult = new JSONResult<>();
        try {
            BranchSpreadEntity result = (BranchSpreadEntity) branchSpreadEntity.selectById(branchSpreadEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesBranchSpreads"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesBranchSpreads")
    public JSONResult<List<BranchSpreadEntity>> getPagesBranchSpreads(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo")Integer pageSize,
            @ApiParam(value = "机构文案营销实体类", name = "branchSpread") BranchSpreadEntity branchSpreadEntity) {
        JSONResult<List<BranchSpreadEntity>> jsonResult = new JSONResult<>();
        try {
            Page<BranchSpreadEntity> page = new Page<>(pageNo, pageSize);
            IPage<BranchSpreadEntity> result = this.service.selectPagesSpread(page, branchSpreadEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }
}
